package marchsoft.modules.tiku.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.parser.ParserConfig;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.annotation.rest.AnonymousDeleteMapping;
import marchsoft.annotation.rest.AnonymousGetMapping;
import marchsoft.annotation.rest.AnonymousPostMapping;
import marchsoft.annotation.rest.AnonymousPutMapping;
import marchsoft.base.PageVO;

import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.tiku.entity.ESafeType;
import marchsoft.modules.tiku.entity.ESlideshow;

import marchsoft.modules.tiku.entity.dto.ERepositoryAndPracticeRecordDTO;
import marchsoft.modules.tiku.entity.dto.ERepositoryDto;
import marchsoft.modules.tiku.entity.dto.SelectRepositoryDto;
import marchsoft.modules.tiku.entity.excel.RepositoryExcel;
import marchsoft.modules.tiku.listener.RepositoryDataListen;
import marchsoft.modules.tiku.service.mapstruct.RepositoryMapStruct;
import marchsoft.modules.tiku.validation.ValidateId;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.omg.SendingContext.RunTime;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import marchsoft.modules.tiku.service.IERepositoryService;
import marchsoft.modules.tiku.entity.ERepository;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
* <p>
* 题库 前端控制器
* </p>
* @author zsy
* @since 2022-03-29
*/
/**
 * <p>
 * 题库 前端控制器
 * </p>
 * @author zsy
 * @since 2022-03-29
 */
@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "题库模块")
@CacheConfig(cacheNames = "repository")
@RequestMapping("/api/eRepository")
public class ERepositoryController {
    private final IERepositoryService eRepositoryService;
    private final RepositoryMapStruct repositoryMapStruct;

    /**
     * 返回题库和练习记录
     *
     * */
    @ApiOperation(value = "按安全分类id查询")
    @GetMapping(value = "/getBySafeTypeId")
    public Result<ERepositoryAndPracticeRecordDTO> getRepositoryBySafeTypeId(Long id){
        if(id == null || id<=0){
            throw new BadRequestException("id不能为空，且id不能小于0");
        }
        ERepositoryAndPracticeRecordDTO eRepositoryAndPracticeRecordDTO=eRepositoryService.getRepositoryAndPracticeRecordIdBySafeTypeId(id);
        return Result.success(eRepositoryAndPracticeRecordDTO);
    }
//    @AnonymousGetMapping("/getBySafeTypeId")
//    public Result<IPage<ERepository>> getById(PageVO pageVO ,Long id){
//        IPage<ERepository> eRepository= eRepositoryService.getBySafeTypeId(pageVO,id);
//        return Result.success(eRepository);
//    }
    @ApiOperation("导入文件")
    @AnonymousPostMapping("/import")
    @ResponseBody
    public Result<Object> importFile(@RequestPart MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), RepositoryExcel.class,new RepositoryDataListen(eRepositoryService)).sheet().doRead();
        return Result.success();
    }
    @ApiOperation("导出用户数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, SelectRepositoryDto repositoryDto) throws IOException {
        //每页条数设置为-1，是查询全部
        PageVO pageVO = new PageVO();
        pageVO.setSize(-1);
        Result<IPage<SelectRepositoryDto>> pageResult = getAll(repositoryDto, pageVO);
        List<SelectRepositoryDto> repositoryDtoList = pageResult.getData().getRecords();
        eRepositoryService.download(repositoryDtoList, response);
    }
    @ApiOperation("查询题目")
    @GetMapping
//    @Cacheable(key = "'all:'+#pageVO.current")
    public Result<IPage<SelectRepositoryDto>> getAll(SelectRepositoryDto repositoryDto,PageVO pageVO){
        IPage<SelectRepositoryDto> all=eRepositoryService.getAll(repositoryDto,pageVO);
//        System.out.println(repositoryDto);

//        IPage<ERepository> all=eRepositoryService.page(pageVO.buildPage());
        return Result.success(all);
    }
    @ApiOperation("新增题目")
    @PostMapping
    @CacheEvict(key = "'all'",allEntries = true)
    public Result<Object> addOne(@RequestBody @Validated ERepositoryDto repository){
        ERepository eRepository= repositoryMapStruct.toRepositoryDto(repository);
        eRepositoryService.save(eRepository);
        return Result.success();
    }
    @ApiOperation("删除题目")
    @DeleteMapping
    @CacheEvict(key = "'all'",allEntries = true)
    public Result<Object> deleteById(@RequestBody Set<Long> ids){
        if(CollectionUtil.isEmpty(ids)){
            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
        }
        eRepositoryService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改题目")
    @PutMapping
    @CacheEvict(key = "'all'",allEntries = true)
    public Result<Object> updateQues(@RequestBody @Validated ERepository eRepository){
        eRepositoryService.updateById(eRepository);
        return Result.success();
    }



}
